#!/bin/sh
#110505 support sudo for non-root user.
#120408 support minit
#140622 shinobar avoid freeze on a virtual terminal

script=${0##*/}

. /etc/rc.d/PUPSTATE
[ "$PUPMODE" = "5" ] && [ $PPID -eq 1 ] && touch /tmp/shutdownconfig_results && sync  #skip shutdownconfig

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

case $script in
 poweroff) [ "$DISPLAY" ] && which wmpoweroff &>/dev/null && exec wmpoweroff ;; #avoid freeze on a virtual terminal
 reboot)   [ "$DISPLAY" ] && which wmreboot &>/dev/null && exec wmreboot ;;
esac

/etc/rc.d/rc.shutdown

case $script in
 poweroff)
   EXE='/bin/busybox poweroff'
   [ -f /sbin/minit ] && EXE='hard-reboot POWER_OFF' #note: hard-reboot is part of minit pkg.
 ;;
 reboot)
   EXE='/bin/busybox reboot'
   [ -f /sbin/minit ] && EXE='hard-reboot RESTART' #note, hard-reboot is part of minit pkg.
 ;;
 *) exit ;;
esac

exec ${EXE}

### END ###
